package com.lyl.momanapp.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.Material;

import java.util.ArrayList;

/**
 * 名字 梁亚龙
 * 时间 2017/9/10.
 * QQ邮箱 503934353@qq.com
 */

public class TwoTwoAdapter extends RecyclerView.Adapter<TwoTwoAdapter.MyViewHolder>{
    private int num = 0;
    private ArrayList<Material.ResultBean.DataBean> list = new ArrayList<>();
    public interface OnItemClickLitener
    {
        void onItemClick(TextView view, int position);
    }
    private OnItemClickLitener mOnItemClickLitener;
    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public String[] datas = null;
    public TwoTwoAdapter(String[] datas) {
        this.datas = datas;
    }
    public void showData(ArrayList<Material.ResultBean.DataBean> datalist) {
        list.addAll(datalist);
        this.notifyDataSetChanged();
    }
    //创建新View，被LayoutManager所调用
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_one_one_daohang,viewGroup,false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
        viewHolder.button.setText(datas[position]);
            if (num==position){
                viewHolder.button.setBackgroundColor(Color.parseColor("#F3F3F3"));
            }else {
                viewHolder.button.setBackgroundColor(Color.parseColor("#ffffff"));
            }
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num=position;
                mOnItemClickLitener.onItemClick(viewHolder.button,position);
                TwoTwoAdapter.this.notifyDataSetChanged();
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.length;
    }



    class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView button;

        public MyViewHolder(View itemView) {
            super(itemView);
            button = (TextView) itemView.findViewById(R.id.but_item_one_one);
        }
    }
}
